package mspbots.data.tl.prod.autotask.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.*;
import mspbots.data.tl.prod.autotask.model.*;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskConfigurationItemsService;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskConfigurationItemsServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskConfigurationItemsMapper, TaskAutotaskConfigurationItems>
        implements TaskAutotaskConfigurationItemsService {
    private final TaskAutotaskConfigurationItemsMapper taskAutoConfigurationItemsMapper;
    @Override
    public void sync(ResultEntity content){
        log.debug("Auto task configurationItems receiver content {}", content);
        JsonNode entity = content.getEntity();
        TaskAutotaskConfigurationItems data = TaskAutotaskConfigurationItems.builder()
                .itemsId(super.getLongValue(entity.get("id")))
                .contractId(ObjectUtils.isEmpty(entity.get("contractID")) ? null : entity.get("contractID").asInt())
                .referenceNumber(ObjectUtils.isEmpty(entity.get("referenceNumber")) ? null : entity.get("referenceNumber").asText())
                .referenceTitle(super.getStringValue(entity.get("referenceTitle")))
                .client(ObjectUtils.isEmpty(entity.get("client")) ? null : entity.get("client").asText())
                .notes(super.getStringValue(entity.get("notes")))
                .location(ObjectUtils.isEmpty(entity.get("location")) ? null : entity.get("location").asText())
                .serialNumber(super.getStringValue(entity.get("serialNumber")))
                .monthlyCost(ObjectUtils.isEmpty(entity.get("monthlyCost")) ? null : entity.get("monthlyCost").asDouble())
                .perUseCost(ObjectUtils.isEmpty(entity.get("perUseCost")) ? null : entity.get("perUseCost").asDouble())
                .hourlyCost(ObjectUtils.isEmpty(entity.get("hourlyCost")) ? null : entity.get("hourlyCost").asDouble())
                .numberOfUsers(ObjectUtils.isEmpty(entity.get("numberOfUsers")) ? null : entity.get("numberOfUsers").asDouble())
                .setupFee(ObjectUtils.isEmpty(entity.get("setupFee")) ? null : entity.get("setupFee").asDouble())
                .dailyCost(ObjectUtils.isEmpty(entity.get("dailyCost")) ? null : entity.get("dailyCost").asDouble())
                .createDate(LocalDateTime.parse(entity.get("createDate").textValue(), DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .warrantyExpirationDate(super.getDateTimeValue(entity.get("warrantyExpirationDate")))
                .installDate(super.getDateTimeValue(entity.get("installDate")))
                .productId(super.getLongValue(entity.get("productID")))
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());

        TaskAutotaskConfigurationItems dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskConfigurationItems>lambdaQuery()
                .eq(TaskAutotaskConfigurationItems::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskConfigurationItems::getItemsId, data.getItemsId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }

}
